package com.sugar.persistence.oa.hr;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Controller;

import com.sugar.base.core.IHibernateBaseDaoSupport;
import com.sugar.base.query.PageModel;
import com.sugar.domain.oa.hr.Employee;

@Controller
public class EmployeeDaoBean extends IHibernateBaseDaoSupport<Employee> implements IEmployee {
	
	@Override
	public void deleteEmpDao(Employee emp) throws RuntimeException {
		this.deleteEntityBaseDao(emp) ;
	}

	@Override
	public List<Employee> findEmpDao(String queryString, String whereStr,
			Object[] param, LinkedHashMap<String, String> orderby)
			throws RuntimeException {
	
		return this.findMoreEntityByParam(queryString, whereStr, param, orderby) ;
	}

	@Override
	public PageModel<Employee> getScorllEmpDao(String queryString,
			String whereStr, Object[] param, int firstPage, int pageSize,
			LinkedHashMap<String, String> orderby) throws RuntimeException {
	
		return this.getScrollRecord(queryString, whereStr, param, firstPage, pageSize, orderby);
	}

	@Override
	public Employee loadEmpByIdDao(String id) throws RuntimeException {
	
		return this.getEntityById(Employee.class, id) ;
	}

	@Override
	public void saveEmpDao(Employee emp) throws RuntimeException {
		this.saveEntityBaseDao(emp) ;
	}

	@Override
	public void updateEmpDao(Employee emp) throws RuntimeException {
		this.updateEntityBaseDao(emp) ;
	}

	@Override
	public void deleteSelectEmpDao(Object[] ids) throws RuntimeException {
		this.deleteEntityMoreIdBaseDao(Employee.class, ids) ;
	}

	@Override
	public PageModel<Employee> getProcedureEmpDao() throws RuntimeException {
		
		try {
			Connection conn = this.getCoonection() ;
			
			//创建CallableStatement对象
			CallableStatement cs = conn.prepareCall("{call p_paging(?,?,?,?,?,?)}") ;
			cs.setString(1, "sugar_employee") ;
			cs.setInt(2, 3);
			cs.setInt(3, 5);
			cs.registerOutParameter(4, oracle.jdbc.driver.OracleTypes.INTEGER) ;
			cs.registerOutParameter(5, oracle.jdbc.driver.OracleTypes.INTEGER) ;
			cs.registerOutParameter(6, oracle.jdbc.driver.OracleTypes.CURSOR) ;
			
			cs.execute() ;
			
			//总记录数
			String myrows = cs.getString(4) ;
			//总页数
			int mypageCount = cs.getInt(5) ;
			
			System.out.println("总记录数："+myrows);
			System.out.println("总页数："+mypageCount);
			
			ResultSet rs = (ResultSet) cs.getObject(6) ;
			while(rs.next()) {
				System.out.println("员工编号："+rs.getString(1) 
						+"  员工名称："+rs.getString(2) 
						+ "   员工职位："+rs.getString(3));
			}
			
			conn.close() ;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
